#ifndef _AS_PATH_H_
#define _AS_PATH_H_

#include "ASCommon.h"
//#include <list>

//#include "Vector2D.h"

NS_AS_BEGIN

//class ASPathPoint
//{
//public:
//	ASPathPoint(const Vector2D& pos,float delayTime=0,int ext=0)
//		:_vPos(pos)
//		,_fDelayTime(delayTime)
//		,_iExt(ext)
//	{};
//
//	Vector2D _vPos;
//	float _fDelayTime;
//	int _iExt;
//};

class ASPath
{
private:

	std::vector<Vector2D>            _WayPoints;

	//points to the current waypoint
	//std::vector<Vector2D>::iterator  _curWaypoint;
	//std::vector<Vector2D>::iterator  _cursor;
	unsigned int					_iIndex;
	unsigned int					_iSize;
	int								_iDistance;
	//flag to indicate if the path should be looped
	//(The last waypoint connected to the first)
	bool                           _bLooped;
	bool							_bHasDistance;

public:

	ASPath():_bLooped(false),_bHasDistance(false),_iDistance(0)
	{
		_iIndex = 0;
		_iSize = 0;
		//_curWaypoint = _WayPoints.end();
		//_cursor = _WayPoints.end();
	}

	//constructor for creating a path with initial random waypoints. MinX/Y
	//& MaxX/Y define the bounding box of the path.
	ASPath(int    NumWaypoints,
		double MinX,
		double MinY,
		double MaxX,
		double MaxY,
		bool   looped):_bLooped(looped),_bHasDistance(false),_iDistance(0)
	{
		CreateRandomPath(NumWaypoints, MinX, MinY, MaxX, MaxY);
		
		//_curWaypoint = _WayPoints.begin();
		//_cursor = _WayPoints.begin();
	}

	void	setDistance(int distance){_iDistance = distance;}
	int		getDistance(){return _iDistance;}

	//returns the current waypoint
	const Vector2D&    CurrentWaypoint()const
	{
		//ASAssert( _curWaypoint != _WayPoints.end(),"ASPath::CurrentWaypoint Path empty");
		//return *_curWaypoint;
		ASAssert( _iIndex < _iSize,"ASPath::CurrentWaypoint Invalid ");
		return _WayPoints.at(_iIndex);
	}

	//returns true if the end of the list has been reached
	bool Finished()
	{
		//return !(_cursor != _WayPoints.end());
		return _iIndex>=_iSize;
	}
	bool IsLastWaypoint()
	{
		//std::vector<Vector2D>::iterator it = _curWaypoint;
		//return ++it==_WayPoints.end();
		return (_iIndex+1)==_iSize;
	}

	//moves the iterator on to the next waypoint in the list
	inline void SetNextWaypoint();

	//creates a random path which is bound by rectangle described by
	//the min/max values
	std::vector<Vector2D> CreateRandomPath(int    NumWaypoints,double MinX,double MinY,double MaxX,double MaxY);


	void LoopOn(){_bLooped = true;}
	void LoopOff(){_bLooped = false;}

	//adds a waypoint to the end of the path
	//void AddWayPoint(Vector2D new_point,float delaytime=0,int ext=0){_WayPoints.push_back(ASPathPoint(new_point,delaytime,ext));};
	void AddWayPoint(const Vector2D& point){_WayPoints.push_back(point);_iSize++;}
	//methods for setting the path with either another Path or a list of vectors
	void Set(const std::vector<Vector2D>& new_path)
	{
		_WayPoints = new_path;
		_iIndex = 0;
		_iSize = _WayPoints.size();
		//_curWaypoint = _WayPoints.begin();
		//_cursor = _WayPoints.begin();
	}
	void Set(const ASPath& path)
	{
		_WayPoints=path.GetPath();
		_iIndex = path._iIndex;
		_iSize = path._iSize;
		_iDistance = path._iDistance;
		_bLooped = path._bLooped;
		_bHasDistance = path._bHasDistance;
		//_curWaypoint = _WayPoints.begin();
		//_cursor = _WayPoints.begin();
	}


	void Clear(){
		_WayPoints.clear();
		_iIndex = 0;
		_iSize = 0;
		_iDistance = 0;
		_bLooped = false;
		_bHasDistance = false;
		//_curWaypoint = _WayPoints.end();
		//_cursor = _WayPoints.end();
	}

	const std::vector<Vector2D>& GetPath()const{return _WayPoints;}

};




//-------------------- Methods -------------------------------------------

inline void ASPath::SetNextWaypoint()
{
	ASAssert (_iSize > 0,"ASPath::SetNextWaypoint WayPoints is empty");
	_iIndex++;
	if (_iIndex>=_iSize)
	{
		if (_bLooped)
		{
			_iIndex = 0;
		}
	}
	//if (++_cursor == _WayPoints.end())
	//{
	//	if (_bLooped)
	//	{
	//		_curWaypoint = _WayPoints.begin(); 
	//	}
	//}
	//else
	//{
	//	++_curWaypoint;
	//}
}  

NS_AS_END

#endif